package com.foxzzz.datamemory.service;

import com.foxzzz.datamemory.constant.CommonConstant;
import com.foxzzz.datamemory.entity.ReadRecord;
import com.foxzzz.datamemory.repository.ReadRecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author wujie
 * @Class ReadRecordService
 * @Description
 * @Date 2020/7/28 15:55
 */
@Service
public class ReadRecordService {
    @Autowired
    private ReadRecordRepository repository;
    private final static double[] levelArray = new double[]{0.5, 12, 24, 48, 96, 168, 360};


    private Date getDateFromLevel(Date date, Integer level) {
        return new Date(date.getTime() + (long) levelArray[level] * 1000 * 60);
    }

    public boolean dealComment(Map<String, Object> map) {
        Map<String, Object> commentTableMap = (Map<String, Object>) map.get("commentable");
        String docName = commentTableMap.get("title").toString();
//        if (!docName.startsWith("[读书笔记]")) {
//            return false;
//        }
        String html = map.get("body_html").toString();
        String path = map.get("path").toString();
        String updatedStr = map.get("updated_at").toString().replace("T", " ").replace("Z", "");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:MM:ss.SSS");
        ReadRecord readRecord = new ReadRecord();
        try {
            Date parse = sdf.parse(updatedStr);
            System.out.println(parse);
            readRecord.setCreateDate(parse);
            readRecord.setLastReview(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String id = map.get("id").toString();
        String docId = commentTableMap.get("id").toString();
        readRecord.setId(id);
        readRecord.setDocName(docName);
        readRecord.setDocId(docId);
        readRecord.setText(html);
        readRecord.setPath(path);
        readRecord.setState(CommonConstant.Y);
        try {
            Pattern compile = Pattern.compile("\\[.*?\\]");
            Matcher matcher = compile.matcher(html);
            matcher.find();
            String group = matcher.group(0);
            readRecord.setTitle(group);
        } catch (Exception e) {

        }
        readRecord.setReviewTime(0);
        readRecord.setLastNeedReview(getDateFromLevel(readRecord.getLastReview(), 0));
        System.out.println(readRecord);
        repository.save(readRecord);
        return false;
    }

    public Page<ReadRecord> findAllNeedVisit(int page, int size) {
        return repository.findAllNeedVisit(new Date(), PageRequest.of(page, size, Sort.by(Sort.Order.asc("docName"), Sort.Order.desc("id"))));
    }

    public void reset(String id) {
        ReadRecord one = repository.getOne(id);
        one.setReviewTime(0);
        one.setLastReview(new Date());
        one.setState(CommonConstant.Y);
        one.setLastNeedReview(getDateFromLevel(one.getLastReview(), 0));
        repository.save(one);
    }

    public void levelUp(String id) {
        ReadRecord one = repository.getOne(id);
        Integer reviewTime = one.getReviewTime();
        if (reviewTime == levelArray.length) {
            one.setState(CommonConstant.N);
        } else {
            reviewTime++;
            one.setReviewTime(reviewTime);
            one.setState(CommonConstant.Y);
            one.setLastReview(new Date());
            one.setLastNeedReview(getDateFromLevel(one.getLastReview(), reviewTime));
            repository.save(one);
        }
    }

    public static void main(String[] args) {
        Pattern compile = Pattern.compile("\\[.*?\\]");
        Matcher matcher = compile.matcher("sadas[asda]sada");
        matcher.find();
        String group = matcher.group(0);
        System.out.println(group);

    }
}
